import sqlite3
import sys

def execute_sql_from_file(db_path, sql_file_path):
    # 连接到SQLite数据库
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    try:
        # 打开SQL文件并读取内容
        with open(sql_file_path, 'r', encoding='utf-8') as file:
            sql_script = file.read()
        
        # 执行SQL脚本
        cursor.executescript(sql_script)
        conn.commit()
        print("SQL script executed successfully.")
    except Exception as e:
        print(f"An error occurred: {e}")
        conn.rollback()
    finally:
        # 关闭连接
        cursor.close()
        conn.close()

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python script.py <sql_file_path>")
    else:
        sql_file_path = sys.argv[1]
        db_path = 'machines.db'  # 数据库文件路径
        execute_sql_from_file(db_path, sql_file_path)
